package MultiPong;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client extends Thread {

	String IP = "localhost";

	private MultiPongTest multiPongTest = MultiPongTest.getInstance();

	public Client(String IP) {
		this.IP = IP;
	}

	public void run() {
		Socket kkSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		// InetAddress addr;
		String host;

		try {
			// volgende werkt enkel op vaste ethernetkaart ?
			// addr = InetAddress.getByName(IP);
			// addr = InetAddress.getByName("pcb401a.khbo.be");
			// System.out.println("Computer heeft IP adres " + addr.toString());

			host = IP;
			kkSocket = new Socket(host, 4444);
			out = new PrintWriter(kkSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					kkSocket.getInputStream()));
		} catch (UnknownHostException ie) {
			System.err.println("Don't know about host");
			System.exit(1);
		} catch (IOException oe) {
			System.err.println("Couldn't get I/O for the connection");
			System.exit(1);
		}

		// BufferedReader stdIn = new BufferedReader(new
		// InputStreamReader(System.in));
		String fromServer = "test";
		String fromUser;

		try {
			boolean spelen = true;
			while (spelen) {
				out.println("" + multiPongTest.getXpos());
				fromServer = in.readLine();
				//System.out.println(fromServer);
				if(fromServer.equals("klaar")){
					System.out.println("Server: " + fromServer);
				}
				

				//slaap(1000);
			}

			out.close();
			in.close();
			kkSocket.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public static void slaap(int mSec) {
		try {
			Thread.currentThread().sleep(mSec);
		} catch (InterruptedException e) {
		}
	}

}
